package org.charmsoft.files;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.*;

/**
 * @ Author: Allen J L Chen
 * This Class is to implement default Workbook loader
 * ${DATE}
 **/
public class DefaultWorkBookLoader implements WorkBookLoader{

    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultWorkBookLoader.class);

    @Override
    public Map<String, XSSFWorkbook> load(File[] files) {
        Map<String, XSSFWorkbook> workbooks = new HashMap();

        Arrays.stream(files).forEach(file ->{
            FileInputStream inputStream = null;
            try {
                inputStream = new FileInputStream(file.getAbsolutePath());
                XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
                workbooks.put(file.getName(),xssfWorkbook);
            } catch (IOException e) {
                LOGGER.error(e.getMessage());
            } finally {
                if(inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        LOGGER.error(e.getMessage());
                    }
                }
            }
        });
        return workbooks;
    }
}
